package com.kmust.dream.wanderingdream.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kmust.dream.wanderingdream.comm.pojo.ResultBean;
import com.kmust.dream.wanderingdream.comm.pojo.ReturnMessage;
import com.kmust.dream.wanderingdream.product.dto.PropertyDTO;
import com.kmust.dream.wanderingdream.product.entity.ProductProperty;
import com.kmust.dream.wanderingdream.product.entity.ProductPropertyName;
import com.kmust.dream.wanderingdream.product.entity.ProductPropertyValue;
import com.kmust.dream.wanderingdream.product.entity.ProductSku;
import com.kmust.dream.wanderingdream.product.service.ProductPropertyNameService;
import com.kmust.dream.wanderingdream.product.service.ProductPropertyService;
import com.kmust.dream.wanderingdream.product.service.ProductPropertyValueService;
import com.kmust.dream.wanderingdream.product.service.ProductSkuService;
import com.kmust.dream.wanderingdream.product.utils.ReturnUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/product/productChoice")
@Api(value = "商品选择API")
public class ProductChoiceController {
    @Autowired
    ProductPropertyService propertyService;
    @Autowired
    ProductPropertyNameService nameService;
    @Autowired
    ProductPropertyValueService valueService;
    @Autowired
    ProductSkuService productSkuService;


    @ApiOperation(value = "获取所有的商品选择", notes = "参数是商品主键productId")
    @GetMapping("/getChoice/{productId}")
    public ResultBean getchoice(@PathVariable Integer productId) {
        PropertyDTO propertyDTO = new PropertyDTO();
        //创建列表，即结果
        List<Map<String, List<ProductPropertyValue>>> res = new ArrayList<>();
        //根据productId获取所有的names
        ResultBean propertyNames = nameService.getPropertyNames(productId);
        if (propertyNames.getStatus().equals(ReturnMessage.GET_SUCCESS.getCode())) {
            Object payload = propertyNames.getPayload();
            for (ProductPropertyName itemName : ((List<ProductPropertyName>) payload)) {
                String key = itemName.getProductPropertyTitle();
                List<ProductProperty> list = propertyService.list(new QueryWrapper<ProductProperty>()
                        .eq("product_id", productId)
                        .eq("product_property_name_id", itemName.getProductPropertyNameId()));
                List<Integer> temp = new ArrayList<>();
                for (ProductProperty productProperty : list) {
                    temp.add(productProperty.getProductPropertyValueId());
                }
                //已经获取到了所有的valueId的列表
                List<ProductPropertyValue> valueList = valueService.list(new QueryWrapper<ProductPropertyValue>()
                        .in("product_property_value_id", temp));
                HashMap<String, List<ProductPropertyValue>> itemMap = new HashMap<>();
                itemMap.put(key, valueList);
                res.add(itemMap);
            }
            propertyDTO.setList(res);
            HashMap<String, ProductSku> map = new HashMap<>();
            QueryWrapper<ProductSku> eq = new QueryWrapper<ProductSku>().eq("product_id", productId)
                    .ne("product_sku_stock", 0);
            List<ProductSku> list = productSkuService.list(eq);
            if (list != null && list.size() != 0) {
                for (ProductSku productSku : list) {
                    String productSkuProperties = productSku.getProductSkuProperties();
                    map.put(productSkuProperties, productSku);
                }
            }
            propertyDTO.setMap(map);
            return ReturnUtils.getOne(propertyDTO);
        }
        return ReturnUtils.getOne(null);
    }
}
